//
//  FQDMainViewController.m
//  FakeQDaily
//
//  Created by mewxiv on 8/20/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDMainViewController.h"
#import "FQDMainMenuViewController.h"
#import "FQDHomeFeedViewController.h"
#import "FQDMainQButton.h"
#import "UIView+FQDExtension.h"
#import "UIImage+ImageEffects.h"

#import "Masonry.h"

@interface FQDMainViewController ()
// 顶层视图控制器
@property (nonatomic, strong) UIViewController *topViewController;
@end

@implementation FQDMainViewController

#pragma mark - Life Cycle

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    [self setupAppearanceStyle];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupMainQButton];
    [self setTopViewController:self.homeFeedVC];
}

#pragma mark - Private methods

- (void)setupAppearanceStyle
{
    UIPageControl *pageControl = [UIPageControl appearance];
    [pageControl setCurrentPageIndicatorTintColor:FQDHighlightColor];
    [pageControl setPageIndicatorTintColor:RGB(69, 75, 91)];
}

- (void)setupMainQButton
{
    self.qButton = [[FQDMainQButton alloc] init];
    [self.qButton addTarget:self action:@selector(qButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.qButton];
    
    WeakSelf;
    [self.qButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view.mas_bottom).offset(-74);
        make.left.equalTo(weakSelf.view).offset(20.0f);
        make.size.equalTo(@54);
    }];
}

#pragma mark - View Controllers management
- (void)setTopViewController:(UIViewController *)topViewController
{
    if (_topViewController == topViewController) {
        return;
    }
    
    [_topViewController removeFromParentViewController];
    [_topViewController.view removeFromSuperview];
    
    _topViewController = topViewController;
    
    [self addChildViewController:topViewController];
    [self.view insertSubview:topViewController.view belowSubview:self.qButton];
}

- (void)presentMenuVC:(BOOL)animated
{
    if (!self.menuVC) {
        self.menuVC = [[FQDMainMenuViewController alloc] init];
        self.menuVC.qButton = self.qButton;
        [self addChildViewController:self.menuVC];
        [self.view insertSubview:self.menuVC.view belowSubview:self.qButton];
        
        if (animated) {
            [self.menuVC animatingOpenWithCompletion:nil];
        }
    }
}

- (void)dismissMenuVC:(BOOL)animated
{
    if (animated) {
        [self.menuVC animatingCloseWithCompletion:^{
            [self.menuVC removeFromParentViewController];
            [self.menuVC.view removeFromSuperview];
            self.menuVC = nil;
        }];
    } else {
        [self.menuVC removeFromParentViewController];
        [self.menuVC.view removeFromSuperview];
        self.menuVC = nil;
    }
}

#pragma mark - Event response
- (void)qButtonTapped:(FQDMainQButton *)qButton
{
    if (qButton.displayState == FQDMainQButtonStateClose) {
        [qButton playBounceAnimation];
        [self presentMenuVC:YES];
    } else if (qButton.displayState == FQDMainQButtonStateOpen) {
        [qButton playBounceAnimation];
        [self dismissMenuVC:YES];
    } else if (qButton.displayState == FQDMainQButtonStateWillBack) {
        [qButton playRushBackAnimation];
        [self.menuVC contractMainMenu];
    }   
}

#pragma mark - Getters and Setters
- (UIViewController *)childViewControllerForStatusBarStyle
{
    return self.topViewController;
}

- (FQDHomeFeedViewController *)homeFeedVC
{
    if (!_homeFeedVC) {
        _homeFeedVC = [[FQDHomeFeedViewController alloc] init];
    }
    return _homeFeedVC;
}

@end
